<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/easycommon.jsp" %>
<!DOCTYPE html>
<html>
<head>
  <title>任务流转</title>
  <meta charset="utf-8">
  <script type="text/javascript" src="${basepath}/assets/ztree/jquery.ztree.all-3.5.js"></script>
  <link rel="stylesheet" type="text/css" href="${basepath}/assets/ztree/zTreeStyle.css" />
  <style>
    table {
      font-size: 14px
    }
    a{
      text-decoration:none;
    }

  </style>
  <script>
    $(function () {
      var datagrid; //定义全局变量datagrid
      var editRow = undefined; //定义全局变量：当前编辑的行
      datagrid = $('#dataTable').datagrid({
        rownumbers: true,
        //singleSelect:true,
        pagination: true,
        pageSize: 30,
        //singleSelect:false,
        fit: true, //datagrid自适应宽度
        fitColumn: false, //列自适应宽度
        striped: true, //行背景交换
        nowap: true, //列内容多时自动折至第二行
        border: false,
        //idField: 'id', //主键
        url: "${path}/cootask/queryTransTask",
        columns: [[
          {field: '_operate', title: '操作', width:70, formatter: formatOper},
          {field: 'title', title: '任务标题', width: 150},
          {field: 'createUserName', title: '发起人', width: 100},
          {field: 'fromUserName', title: '流转人', width: 100},
          {field: 'createdtimeYMD', title: '流转时间', width: 100},
          {field: 'toUserName', title: '接收人', width: 100},
          //{field: 'content', title: '任务内容', width: 250},
          {field: 'taskTypeName', title: '任务类型', width: 80},
          {field: 'taskLevelName', title: '任务等级', width: 80},
          {field: 'beginTime', title: '开始时间', width: 100},
          {field: 'endTime', title: '结束时间', width: 100},
          {field: 'transferMaxLevel', title: '最大流转次数', width: 100},
          {field: 'transferLevel', title: '当前流转次数', width: 100},
          //{field: 'note', title: '备注', width: 250},
          {field: 'statusName', title: '流转状态', width: 80},
        ]],
        //queryParams: { action: 'query' }, //查询参数
        method:'get',
        toolbar:'#tb'
        /*, toolbar: [
          {
            text: '查询', iconCls: 'icon-search', handler: function () {
              searchFun();
            }
          }] */
      });


    })

    function formatOper(val, row, index) {
      return '<a href="#" onclick="operPop(' + index + ')">查看/处理</a>   ';
              //'<a href="#" onclick="backFun(' + index + ')">退回</a>   ';
    }

    var row;
    function operPop(index) {
      $('#dataTable').datagrid('selectRow', index);
      row = $('#dataTable').datagrid('getSelected');
      if (row) {
        $('#editForm').form('clear');
        $('#editForm').form('load', row);
        $('#editWin').window('open');
      } else {
        $.messager.alert("提示", "请选择要编辑的行", "error");
      }
    }

    function transPop() {
      $('#transWin').window('open');
    }


    //查询
    function searchFun(){
      var options = $('#dataTable').datagrid('options');
      //options.url = '${path}/report/khfxGridData';
      //options.queryParams = { dateYear: $('#querydate').val()};
      $('#dataTable').datagrid(options);
    }

    var layIndex;
    //添加扩展信息
    function tablePop(){
      if (row.id&&row.taskUserId) {
        var fromUserId = row.toUserId;//本级用户
        var fromUserName = row.toUserName;
        var fromDeptId = row.toDeptId;//本级部门
        var fromDeptName = row.toDeptName;
        var url = "${path}/cootasktable/userValue?fTaskHeadid="+row.id+"&fTaskUserid="+row.taskUserId+"&fromUserId="+row.toUserId+
                "&fromUserName="+row.toUserName+"&fromDeptId="+row.toDeptId+"&fromDeptName="+row.toDeptName+"&transferMaxLevel="+row.transferMaxLevel+
                "&transferLevel="+row.transferLevel;
        url = decodeURI(url);
        layIndex = layer.open({
          title:"扩展信息",
          type: 2,
          fix: false,
          maxmin: true,
          area: ['900px', '670px'],
          content: url,
          cancel: function(index){

          }
        });
      } else {
        $.messager.alert("提示", "请先选择一条记录", "error");
      }
    }

    //下发
    function transSubmit(){
      var zTree = $.fn.zTree.getZTreeObj("detptree"),
              nodes = zTree.getChangeCheckedNodes(),
              checkedUserIds = [],checkedUserNames = [],
              checkedUserDeptIds = [],checkedUserDeptNames = [];
      for (var i=0, l=nodes.length; i<l; i++) {
        if(nodes[i].datatype=='u'){
          checkedUserIds.push(nodes[i].id);
          checkedUserNames.push(nodes[i].name);
          checkedUserDeptIds.push(nodes[i].deptId);
          checkedUserDeptNames.push(nodes[i].deptName);
        }
      }
      if (!row.id) {
        return $.messager.alert("提示", "请先选择一条记录", "error");
      }
      if (checkedUserIds.length==0) {
        return $.messager.alert("提示", "请选择下发人员", "error");
      }
      if ($('#transForm').form('enableValidation').form('validate')) {
        var formval = JsonCode(transForm);
        formval = JSON.parse(formval);
        formval.id = row.taskUserId;//流转id
        formval.fTaskHeadid = row.id;//任务id
        formval.fromUserId = row.toUserId;//本级用户
        formval.fromUserName = row.toUserName;
        formval.fromDeptId = row.toDeptId;//本级部门
        formval.fromDeptName = row.toDeptName;
        formval.toUserId = checkedUserIds.join(",");
        formval.toUserName = checkedUserNames.join(",");
        formval.toDeptId = checkedUserDeptIds.join(",");
        formval.toDeptName = checkedUserDeptNames.join(",");
        if(row.transferMaxLevel){//如果存在最大次数限制，则最大次数加1
          if(parseInt(row.transferLevel)<parseInt(row.transferMaxLevel)){
            formval.transferLevel = parseInt(row.transferLevel)+1;
          }else{
            return $.messager.alert("提示", "任务流转已达最大层数，请完结任务", "error");
          }
        }
        //console.log(formval);
        var actionUrl = "${path}/cootask/saveTaskUser";
        $.ajax({
          type: "POST",
          url: actionUrl,
          data: JSON.stringify(formval),
          dataType: 'json',
          contentType: "application/json;charset=utf-8",
          success: function (text) {
            if (text.status == 1) {
              $('#transWin').window('close');
              $('#editWin').window('close');
              $('#dataTable').datagrid('reload');
            }
            layer.msg(text.msg, {time: 3000, icon: 6});
          },
          error: function (jqXHR, textStatus, errorThrown) {
            layer.msg("与远程服务器交互异常", {time: 3000, icon: 6});
          }
        });
      }
    }

    //完结
    var finish = function(){
      if (!row.id) {
        return $.messager.alert("提示", "请先选择一条记录", "error");
      }
      $.messager.confirm("提示", "完结后任务将结束流转，是否进行此操作?", function (r) {
        if (r) {
          var formval = {};
          formval.id = row.taskUserId;//流转id
          formval.fTaskHeadid = row.id;//任务id
          var actionUrl = "${path}/cootask/finishTaskUser";
          $.ajax({
            type: "GET",
            url: actionUrl,
            data: formval,
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            success: function (text) {
              if (text.status == 1) {
                $('#editWin').window('close');
                $('#dataTable').datagrid('reload');
              }
              layer.msg(text.msg, {time: 3000, icon: 6});
            },
            error: function (jqXHR, textStatus, errorThrown) {
              layer.msg("与远程服务器交互异常", {time: 3000, icon: 6});
            }
          });
        }
      })
    }

    function closeAll(doc){
      if(layIndex){
        layer.close(layIndex);
      }
      $('#editWin').window('close');
      $('#dataTable').datagrid('reload');
      layer.msg(doc.msg, {time: 3000, icon: 6});
    }

    $(document).ready(function(){
      var settingmenu = {
        data: {
          simpleData: {
            enable: true
          }
        },
        check: {
          enable: true,
          nocheckInherit: true
        }
      };

      $.ajax({
        url: "${path}/commonSel/easyTreeDeptUser?dept_id=${deptId}",
        success: function (text) {
          $.fn.zTree.init($("#detptree"), settingmenu, text);
        },
        error: function (jqXHR, textStatus, errorThrown) {
          toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
        }
      });
    })

		var searchContent = function() {
			var seachform = $('#searchForm').form();
			$('#dataTable').datagrid('load', serializeObject(seachform));
		}

		
		</script>
</head>
<body>
<table id="dataTable" title="任务流转" class="easyui-datagrid" style="width:850px;height:450px"
       singleSelect="true" iconCls="icon-save">
</table>

<div id="editWin" class="easyui-window" title="任务信息" style="width:620px;height:480px;padding:10px;"
     data-options="iconCls:'icon-save',modal:true,closed:true,top:100">
  <form id="editForm" name="editForm" method="post">
    <input type="hidden" name="id"/>
    <table cellpadding="5">
      <tr>
        <td>标题:</td>
        <td><input  readonly="readonly"  class="easyui-textbox" type="text" name="title" style="width:150px"/></td>
        <td>发起人:</td>
        <td><input  readonly="readonly"  class="easyui-textbox" type="text" name="createUserName" style="width:150px"/></td>
      </tr>
      <tr>
        <td>流转人:</td>
        <td><input  readonly="readonly"  class="easyui-textbox" type="text" name="fromUserName" style="width:150px"/></td>
        <td>接收人:</td>
        <td><input  readonly="readonly"  class="easyui-textbox" type="text" name="toUserName" style="width:150px"/></td>
      </tr>
      <tr>
        <td>类型:</td>
        <td>
          <input  readonly="readonly"  class="easyui-combobox" id="taskType" name="taskType" style="width:150px;" data-options="
                            url:'${path}/common/getBaseCodeList?type=coo_task_type',
                            valueField: 'code',
                            textField: 'name',
                            required:false
                            ">
        </td>
        <td>等级:</td>
        <td>
          <input  readonly="readonly"  class="easyui-combobox" id="taskLevel" name="taskLevel" style="width:150px;" data-options="
                            url:'${path}/common/getBaseCodeList?type=coo_task_level',
                            valueField: 'code',
                            textField: 'name',
                            required:false
                            ">
        </td>
      </tr>
      <tr>
        <td>最大流转次数:</td>
        <td><input readonly="readonly" class="easyui-numberbox" type="text" name="transferMaxLevel" style="width:150px"
                   data-options="required:false" value="0"/></td>
        <td>当前流转次数:</td>
        <td><input  readonly="readonly"  class="easyui-numberbox" type="text" name="transferLevel" style="width:150px"
                   data-options="required:false" value="0"/></td>
      </tr>
      <tr>
        <td>开始时间:</td>
        <td>
          <input  readonly="readonly"  type="text" name="beginTime" id="beginTime" class="mytextbox"
                 onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">
        </td>
        <td>结束时间:</td>
        <td>
          <input  readonly="readonly"  type="text" name="endTime" id="endTime" class="mytextbox"
                 onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">
        </td>
      </tr>
      <tr>
        <td>内容:</td>
        <td colspan="3"><input  readonly="readonly"  class="easyui-textbox" name="content" data-options="multiline:true"
                   style="height:80px;width:420px"/></td>
      </tr>
      <tr>
        <td>任务备注:</td>
        <td colspan="3"><input  readonly="readonly"  class="easyui-textbox" name="note" data-options="multiline:true"
                   style="height:80px;width:420px"/></td>
      </tr>
    </table>
    <div style="text-align:right;margin-top:10px;padding-right:54px">
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-tip'" style="width:30%;color:red"
         onclick="tablePop()">填写任务信息</a> 
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-redo'" style="width:15%"
         onclick="transPop()">下发</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="width:20%"
         onclick="finish()">完结/提交</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" style="width:15%"
         onclick="javascript:$('#editWin').window('close')">关闭</a>
    </div>
  </form>
</div>

<div id="transWin" class="easyui-window" title="下发" style="width:680px;height:480px;padding:10px;"
     data-options="iconCls:'icon-save',modal:true,closed:true,top:100">
    <form id="transForm" name="transForm" method="post">
    <table cellpadding="5">
      <tr>
        <td>部门:</td>
        <td>流转备注:</td>
      </tr>
      <tr>
        <td>
          <div style="padding:10px;width:300px;height:300px;overflow-y:auto">
            <ul id="detptree" class="ztree"></ul>
          </div>
        </td>
        <td><input class="easyui-textbox" name="note" data-options="multiline:true"
                   style="height:300px;width:300px;"/></td>
      </tr>
    </table>
    <div style="text-align:right;margin-top:10px;padding-right:33px">
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" style="width:15%"
         onclick="transSubmit()">保存</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" style="width:15%"
         onclick="javascript:$('#transWin').window('close')">关闭</a>
    </div>
    </form>
</div>

<div id="tb" > 
	<form id="searchForm" style="height:13px" >
	<!--<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newPop()">添加</a>  -->

	<input class="easyui-textbox" type="text" id="titles" name="title" style="width:150px"
                   data-options="prompt:'任务名'"/>
                   
<!--     <input class="easyui-textbox" type="text" id="toUserNames" name="toUserName" style="width:150px"
                   data-options="prompt:'接受人'"/> -->
    <input class="easyui-textbox" type="text" id="fromUserNames" name="fromUserName" style="width:150px"
                   data-options="prompt:'流转人'"/>
    
    <input class="easyui-combobox" id="taskTypes" name="taskType" style="width:150px;" 
    	data-options="
            url:'${path}/common/getBaseCodeList?type=coo_task_type',
            valueField: 'code',
            textField: 'name',
			prompt:'任务类型'            
            ">
    <input class="easyui-combobox" id="taskLevels" name="taskLevel" style="width:150px;" 
    	data-options="
            url:'${path}/common/getBaseCodeList?type=coo_task_level',
            valueField: 'code',
            textField: 'name',
			prompt:'任务等级'            
            ">                            

   	<input class="easyui-combobox" id="taskStatuss" name="status" style="width:150px;" 
    	data-options="
            url:'${path}/common/getBaseCodeList?type=coo_task_status',
            valueField: 'code',
            textField: 'name',
			prompt:'任务状态'            
            ">    
                  
    <input type="text" name="beginTime" id="sbeginTime" class="mytextbox" placeholder="开始时间"
              onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">    
    
    <input type="text" name="endTime" id="sendTime" class="mytextbox" placeholder="结束时间"
                 onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">

<!--     <input class="easyui-numberspinner" id="transferMaxLevels" name="transferMaxLevel" style="width:150px"
                   data-options="prompt:'最大流转数',min:0"/> -->
	                     
    <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="searchContent()">搜索</a>  
	</form>
</div>
</body>
</html>
